﻿using Azylee.Core.AppUtils;
using Azylee.Core.DataUtils.StringUtils;
using Azylee.Core.IOUtils.TxtUtils;
using Azylee.Core.LogUtils.StatusLogUtils;
using JustMetClient.Commons;
using JustMetClient.Modules.GCModule;
using JustMetClient.Modules.PlanTaskModule;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace JustMetClient
{
    static class Program
    {
        static AppUnique AppUnique = new AppUnique();
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //解决进程互斥
            if (!AppUnique.IsUnique(R.AppName)) return;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            R.MainUI = new MainForm();
            //执行任务
            PlanTaskCore.Start();
            AutoGC.Start();
            InitData();
            Application.Run(R.MainUI);

        }


        public static void InitData()
        {
            R.NetInfo.Default();//获取默认的IP地址配置
            StatusLog.Instance.Start();//启动计算机状态日志记录
            R.Log.SetCacheDays(5);//保存最近5天的普通日志信息
            StatusLog.Instance.SetCacheDays(5);//保存最近5天的状态日志信息
          
            #region 记录运行次数
            //记录运行次数（int转换）
            R.RunTimes = (int)IniTool.GetLong(R.Files.Settings, "Run", "Times", 0);
            R.RunTimes++;
            IniTool.Set(R.Files.Settings, "Run", "Times", R.RunTimes.ToString());
            #endregion

            #region 记录当前运行程序路径
            //记录当前运行程序路径
            IniTool.Set(R.Files.Settings, "File", "JustMetClient.exe", R.Files.App);
            #endregion

            #region 记录当前运行程序的版本
            //记录当前运行程序的版本
            IniTool.Set(R.Files.Settings, "Version", "JustMetClient-Current", R.Version.ToString());
            #endregion

            #region 记录历史版本

            //记录历史版本
            string history = IniTool.GetString(R.Files.Settings, "Version", "JustMetClient-History", "");
            if (!history.Contains($"|{R.Version}|"))
            {
                IniTool.Set(R.Files.Settings, "Version", "JustMetClient-History", history + $"|{R.Version}|");
            }
            #endregion

            #region 设置本机唯一ID（重装会更新）

            //设置本机唯一ID（重装会更新）
            R.AppId = IniTool.GetString(R.Files.Settings, "User", "AppId", "");
            if (StringTool.IsNullOrWhiteSpace(R.AppId))
            {
                R.AppId = Guid.NewGuid().ToString();
                IniTool.Set(R.Files.Settings, "User", "AppId", R.AppId);
            }
            #endregion

            #region 剪切板配置
            //初始化剪切板数据存储路径
            string dataPath = IniTool.GetString(R.Files.Settings, "dataPath", "BigClipboard-dataPath", "");
            if (String.IsNullOrEmpty(dataPath))
            {
                IniTool.Set(R.Files.Settings, "dataPath", "BigClipboard-dataPath", R.Paths.DefaultDataPath);
                dataPath = R.Paths.DefaultDataPath;
            }
            //初始化自定义缓存路径
            R.Paths.MyDataPath = dataPath;
            #endregion

            #region 是否为虚拟机
            //获取计算机信息-是否为虚拟机（虚桌面）
            bool virtual_computer = IniTool.GetBool(R.Files.Install, "Computer", "Virtual", false);
            R.Flag.IsVirtualComputer = virtual_computer;
            #endregion

            #region 是否可以写出文件（检测权限）
            if (File.Exists(R.Files.Settings)) R.ICanWriteFile = true;
            #endregion

            #region 颜色配置
            //记录当前运行程序的配置
            var BackColor = IniTool.GetString(R.Files.Settings, "Settings", "BackColor", "captiontext");
            if (string.IsNullOrEmpty(BackColor.ToString()))
            {
                IniTool.Set(R.Files.Settings, "Settings", "BackColor", "#2f4f4f");
                R.BackColor = ColorTranslator.FromHtml(BackColor);
            }
            //设置主题颜色
            try
            {
                R.BackColor = ColorTranslator.FromHtml(BackColor);
            }
            catch
            {
                IniTool.Set(R.Files.Settings, "Settings", "BackColor", "#2f4f4f");
            }
            finally
            {
                InitMainView();
            }
            #endregion

            #region 将启动器添加到开机启动
            //将启动器添加到开机启动
            //bool regFlag = StartupTool.Register("JustMetClient.Launch.exe", Application.ExecutablePath, true, false);
            #endregion
        }


        //初始化MainView
        public static void InitMainView()
        {
            R.MainUI.SingleTitleFormPNHead.BackColor = R.BackColor;
            R.MainUI.panel1.BackColor = R.BackColor;
        }
    }
}
